package com.doule.farmerassistant.plan;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.doule.farmerassistant.R;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

/**
 * Plan adapter.
 * 
 * @author riguang
 * 
 */
public class PlanAdapter extends BaseAdapter {
    /**
     * PlanDetail table.
     */
    private final ArrayList<PlanDetail> mData;
    /**
     * View.
     */
    private LayoutInflater inflater = null;

    /**
     * PlanFragment context.
     */
    private PlanFragment mContext;

    /**
     * Group id.
     */
    // private String mGroupId = null;
    // /**
    // * admin id.
    // */
    // private String mAdmin = null;

    // /**
    // * member list.
    // */
    // private List<String> mMembers = null;

    /**
     * ContactBaseAdapter construct.
     * 
     * @param activity
     *            MessengerActivity
     * @param data
     *            Data of Contact
     */
    public PlanAdapter(final PlanFragment activity,
            final ArrayList<PlanDetail> data) {
        mData = data;
        mContext = activity;
        inflater = LayoutInflater.from(activity.getActivity());
    }

    /**
     * get Contact list.
     * 
     * @return mData
     */
    public final ArrayList<PlanDetail> getContactList() {
        return mData;
    }

    @Override
    public final int getCount() {
        return mData.size();
    }

    @Override
    public final Object getItem(final int position) {
        return mData.get(position);
    }

    @Override
    public final long getItemId(final int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public final View getView(final int position, final View convertView,
            final ViewGroup parent) {
        final ViewHolder viewHolder;
        View vi = convertView;
        final PlanDetail data = mData.get(position);
        // final String id = data.getDeviceId();
        if (convertView == null) {
            viewHolder = new ViewHolder();
            vi = inflater.inflate(R.layout.plan_row, null);
            viewHolder.checkBox = (TextView) vi.findViewById(R.id.tv_plan_name);
            viewHolder.row = (RelativeLayout) vi
                    .findViewById(R.id.rl_show_plan_row);
            viewHolder.inf = (RelativeLayout) vi
                    .findViewById(R.id.rl_show_plan_information);
            viewHolder.time = (TextView) vi
                    .findViewById(R.id.tv_show_plan_time);
            viewHolder.tasks = (TextView) vi
                    .findViewById(R.id.tv_show_plan_tasks);
            viewHolder.mSwitch = (Switch) vi.findViewById(R.id.switch_one);
            vi.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) vi.getTag();
        }
        viewHolder.row.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                if (viewHolder.inf.getVisibility() == View.GONE) {
                    viewHolder.inf.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.inf.setVisibility(View.GONE);
                }
                return false;
            }
        });
        final int minPerHour = 60;
        int dayType = data.getDayType();
        String ti = "";
        String timeS = data.getStartTime() / minPerHour + ":" + data.getStartTime()
                % minPerHour;
        if (dayType == 1) {
            ti = mContext.getString(R.string.rb_everyday) + "   " + timeS;
        } else if (dayType == 2) {
            ti = timeS;
        } else if (dayType == 3) {
            ti = mContext.getString(R.string.week) + data.getDay() + "   "
                    + timeS;
        }
        viewHolder.data = data;
        String st = data.getPlanName();
        viewHolder.checkBox.setText(st);
        viewHolder.time.setText(ti);
        String names = getPlanNames(data.getTaskNames());
        viewHolder.tasks.setText(names);
        if (data.getState() == 1) {
            viewHolder.mSwitch.setChecked(true);
        } else if (data.getState() == 0) {
            viewHolder.mSwitch.setChecked(false);
        }
        viewHolder.mSwitch
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean isChecked) {
                        // TODO Auto-generated method stub
                        if (isChecked) {
                            StartPlanTask g = new StartPlanTask(mContext
                                    .getActivity(),
                                    viewHolder.data.getPlanID(), mContext
                                            .getToken());
                            g.go();
                        } else {
                            StopPlanTask g = new StopPlanTask(mContext
                                    .getActivity(),
                                    viewHolder.data.getPlanID(), mContext
                                            .getToken());
                            g.go();
                        }
                    }
                });
        return vi;
    }

    /**
     * Parser plan names from jsonArray.
     * @param ja
     * json array.
     * @return
     * names of plan
     */
    private String getPlanNames(final JSONArray ja) {
        String names = "";
        for (int i = 0; i < ja.length(); i++) {
            try {
                if (i != ja.length() - 1) {
                    names = names + ja.get(i).toString() + " , ";
                } else {
                    names = names + ja.get(i).toString();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return names;
    }

    /**
     * @author Doule
     */
    private class ViewHolder {

        /**
         * checkbox for select or unselect.
         */
        private TextView checkBox;

        /**
         * data binding to this view holder.
         */
        private PlanDetail data;
        /**
         * 
         */
        private RelativeLayout row, inf;
        /**
         * 
         */
        private TextView time, tasks;
        /**
         * Switch view.
         */
        private Switch mSwitch;
    }

}
